package com.dianrui.operation.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.dianrui.operation.Constant;
import com.dianrui.operation.activity.VehicleSchedulingMainActivity;
import com.dianrui.operation.event.AlarmScheduing;
import com.dianrui.operation.util.SPUtils;


import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

import static android.content.Context.MODE_PRIVATE;

/**
 * 自定义接收器
 * <p>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 * @author lzy
 */
@SuppressWarnings("ALL")
public class ReceivedMessage extends BroadcastReceiver {
    private static final String TAG = "ReceivedMessage";
    SPUtils spUtils;

    @Override
    public void onReceive(final Context context, Intent intent) {
        spUtils = new SPUtils(context, Constant.cfgFileName, MODE_PRIVATE);

        try {
            Bundle bundle = intent.getExtras();
            Log.e(TAG, intent.getAction() + "\n[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

            Log.e(TAG, bundle.getString(JPushInterface.EXTRA_EXTRA));
            if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                //接收到推送下来的通知
                Log.e(TAG, "[MyReceiver] 接收到推送下来的通知");
                if (bundle != null) {
                    String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

                    if (!ExampleUtil.isEmpty(extras)) {
                        try {
                            JSONObject extraJson = new JSONObject(extras);
                            extras = extraJson.toString();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    if (!ExampleUtil.isEmpty(extras)) {
                        try {
                            JSONObject extraJson = new JSONObject(extras);
                            extras = extraJson.toString();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    final JSONObject orderJson = new JSONObject(extras);
                    switch (orderJson.optString("type")) {
                        case "alarm":
                            EventBus.getDefault().post(new AlarmScheduing());
                            break;
                            default:
                                break;


                    }

                }
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Log.e(TAG, "[MyReceiver] 用户点击打开了通知");
                String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                if (!ExampleUtil.isEmpty(extras)) {
                    try {
                        JSONObject extraJson = new JSONObject(extras);
                        extras = extraJson.toString();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                if (!ExampleUtil.isEmpty(extras)) {
                    try {
                        JSONObject extraJson = new JSONObject(extras);
                        extras = extraJson.toString();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                final JSONObject alarmJson = new JSONObject(extras);
                if (alarmJson != null) {
                    if (alarmJson.optString("type").equals("alarm")) {
                        Intent intent1 = new Intent(context, VehicleSchedulingMainActivity.class);
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("title", "调度");
                        intent1.putExtras(bundle1);
                        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent1);
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {

                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {

                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }
}
